package ccnt.pvc.scudwaremobile.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import ccnt.pvc.scudwaremobile.R;
import ccnt.pvc.scudwaremobile.bean.ResultObject;
import ccnt.pvc.scudwaremobile.dao.ServiceModel;
import ccnt.pvc.scudwaremobile.storage.ServiceDbManager;
import ccnt.pvc.scudwaremobile.utils.HttpClientUtils;
import ccnt.pvc.scudwaremobile.utils.ThreadPoolUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class CoopServiceListActivity extends Activity {

	private final static String TAG = CoopServiceListActivity.class.getName();
	private final static int QUERY_SUCCESS = 1;
	
	String account;
	String ip;
	
	TextView textOwner;
	ListView listService;
	
	ServiceDbManager serviceDbManager = null;
	List<ServiceModel> regServices = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_coop_servicelist);
		
		Bundle bundle = getIntent().getExtras();
		account = bundle.getString("account");
		ip = bundle.getString("ip");
		
		initService();
		initView();
	}
	
	private void initService() {
		serviceDbManager = new ServiceDbManager(CoopServiceListActivity.this);
	}
	
	private void initView() {
		textOwner = (TextView)findViewById(R.id.textOwner);
		StringBuilder builder = new StringBuilder();
		builder.append("帐户：" + account + "\n");
		builder.append("IP：" + ip + "\n");
		builder.append("\n该中间件提供的服务：" + "\n");
		textOwner.setText(builder.toString());
		listService = (ListView)findViewById(R.id.listService);
		
		
		//fillServiceList();
		ThreadPoolUtils.execute(new Runnable() {
			
			@Override
			public void run() {
				getServices();
			}
		});
	}
	
	private void fillServiceList() {
		//regServices = serviceDbManager.getAllRegServices();
		ArrayList<String> serviceNames = new ArrayList<String>();
		for(ServiceModel service : regServices) {
			serviceNames.add(service.getClassName());
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				CoopServiceListActivity.this, android.R.layout.simple_list_item_1,
				serviceNames);
		listService.setAdapter(adapter);	
		listService.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(CoopServiceListActivity.this, CoopServiceDetailActivity.class);
				intent.putExtra("className", regServices.get(arg2).getClassName());
				intent.putExtra("isReg", true);
				intent.putExtra("account", account);
				intent.putExtra("ip", ip);
				intent.putExtra("serviceModel", new Gson().toJson(regServices.get(arg2)));
				startActivity(intent);
			}
		});
	}
	
	private void getServices() {
		Map<String, Object> params = new HashMap<String, Object>();
		ResultObject resultObject = HttpClientUtils.sendGet("http://" + ip + ":8080/service/all.json", params);
		int code = resultObject.getCode();
		if(code == 200) {
			String result = resultObject.getResult();
			Log.d(TAG, resultObject.getResult());
			regServices = new Gson().fromJson(result,
					new TypeToken<List<ServiceModel>>() {
					}.getType());
			handler.sendMessage(handler.obtainMessage(QUERY_SUCCESS));
		}
	}
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what) {
			case QUERY_SUCCESS:
				fillServiceList();
				break;
			default:
				super.handleMessage(msg);
			}
			
		}
		
	};
}
